package com.sys.core.shiro;

import com.alibaba.fastjson.JSONObject;
import com.sys.user.domain.XyMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/6/19.
 */
public class MenuCache {
    //Map<标识,菜单链接>
    private static Map<String,String> menu_cache = new HashMap<String, String>();//后台菜单缓存,网页拦截缓存，根据网址找到实体，并和shiro匹配是否认证，如果认证则通过
    //Map<菜单id,菜单bean>
    private static Map<String,XyMenu> menuBeanById_cache = new HashMap<String, XyMenu>();
    //Map<标识,菜单bean>
    private static Map<String,XyMenu> menuBeanByNameEn_cache = new HashMap<String, XyMenu>();
    /** 通过key获取菜单实体 */
    public static XyMenu getMenu_cache(String key) {
        String map = menu_cache.get(key);
        XyMenu menu = JSONObject.parseObject(map, XyMenu.class);
        return menu;
    }

    public static Map<String, String> getMenu_cache() {
        return menu_cache;
    }
    /** 全更新 */
    public static void setMenu_cache(List<XyMenu> menuList) {
        Map<String, String> mapstr = new HashMap<String, String>();
        for (XyMenu menu : menuList) {
            mapstr.put(menu.getUrl(),JSONObject.toJSONString(menu));
        }
        MenuCache.menu_cache = mapstr;
    }
    /** 单独更新或者添加某个菜单缓存 */
    public static void saveOrUpdateMenu(XyMenu xyMenu) {
        MenuCache.menu_cache.put(xyMenu.getUrl(), JSONObject.toJSONString(xyMenu));
    }
    /** 移除某个菜单缓存 */
    public static void removeMenu(XyMenu xyMenu) {
        MenuCache.menu_cache.remove(xyMenu.getNameEn());
    }

    public static XyMenu getMenuBeanById_cache(String menuId) {
        return menuBeanById_cache.get(menuId);
    }

    public static void setMenuBeanById_cache(List<XyMenu> menuBeanByIdList) {
        menuBeanById_cache = new HashMap<String, XyMenu>();
        for (XyMenu xyMenu : menuBeanByIdList) {
            menuBeanById_cache.put(xyMenu.getMenuId(), xyMenu);
        }
    }

    public static XyMenu getMenuBeanByNameEn_cache(String nameEn) {
        return menuBeanByNameEn_cache.get(nameEn);
    }

    public static void setMenuBeanByNameEn_cache(List<XyMenu> menuBeanByIdList) {
        menuBeanByNameEn_cache = new HashMap<String, XyMenu>();
        for (XyMenu xyMenu : menuBeanByIdList) {
            menuBeanByNameEn_cache.put(xyMenu.getNameEn(), xyMenu);
        }
    }
}
